package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetCtypeBean;

/**
 * Created by chenchangxing on 15/12/25.
 */
public class InfoItemAdapter extends RecyclerView.Adapter {

    int selection = 1000, value;
    OnItemClickListener onItemClickListener;
    List<GetCtypeBean.DataEntity> list;
    Context context;
    ViewHolder myHolder;


    public InfoItemAdapter(List<GetCtypeBean.DataEntity> list) {
        this.list = list;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        public void onClick(int position, String id);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_info_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        myHolder = (ViewHolder)holder;
        myHolder.itemInfoTips.setText(list.get(position).getTitle());
        myHolder.itemInfoTips.setTag(list.get(position).getId());
        if (selection == position) {
            myHolder.itemInfoLayout.setBackgroundResource(R.drawable.bg_main_rank);
            myHolder.itemInfoTips.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        } else {
            myHolder.itemInfoLayout.setBackgroundResource(0);
            myHolder.itemInfoTips.setTextColor(context.getResources().getColor(R.color.title));
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position, list.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView itemInfoTips;
        RelativeLayout itemInfoLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            itemInfoTips = (TextView) itemView.findViewById(R.id.item_info_tips);
            itemInfoLayout = (RelativeLayout) itemView.findViewById(R.id.item_info_layout);
        }
    }

    public void setSelection(int selection) {
        this.selection = selection;
    }
}
